﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using MySQLStudio.BusinessLogic;

namespace MySQLStudio.GUI.Controls
{
    public class ScriptWindow : Telerik.WinControls.UI.Docking.DockWindow
    {
        private SyntaxHighlightingTextBox m_txtSQLScript;

        private Host m_host;
        private Database m_database;

        protected String m_SqlStatement = String.Empty;

        public SyntaxHighlightingTextBox ScriptTextBox
        {
            get { return m_txtSQLScript; }
            set { m_txtSQLScript = value; }
        }

        public virtual String SqlStatement
        {
            get { return (m_SqlStatement = m_txtSQLScript.Text); }
            set { m_SqlStatement = value; }
        }

        public Host Host
        {
            get { return m_host; }
        }

        public Database Database
        {
            get { return m_database; }
            set { m_database = value; }
        }

        public string SqlContent
        {
            set
            {
                this.m_txtSQLScript.Text = value;
                this.m_txtSQLScript.OnTextChanged();
            }
        }

        public ScriptWindow(Host host, Database database)
        {
            this.m_host = host;
            this.Database = database;
            this.m_txtSQLScript = GetSQLSyntaxTextBox();
            this.m_txtSQLScript.Dock = DockStyle.Fill;
            this.Controls.Add(this.m_txtSQLScript);
        }

        public void InsertText(String text)
        {
            int insertIndx = ScriptTextBox.SelectionStart;
ScriptTextBox.Text =             ScriptTextBox.Text.Insert(insertIndx, text);
           // ScriptTextBox.Text. += text;
            ScriptTextBox.OnTextChanged();
        }

        private SyntaxHighlightingTextBox GetSQLSyntaxTextBox()
        {
            SyntaxHighlightingTextBox txtSQLScript = new SyntaxHighlightingTextBox();
            txtSQLScript.Font = FontsInUse.SQLScriptFont;

            txtSQLScript.Seperators.Add(' ');
            txtSQLScript.Seperators.Add('\r');
            txtSQLScript.Seperators.Add('\n');
            txtSQLScript.Seperators.Add(',');
            txtSQLScript.Seperators.Add('.');
            txtSQLScript.Seperators.Add('-');
            txtSQLScript.Seperators.Add('+');

            txtSQLScript.HighlightDescriptors = Highlighters.Sql;
            return txtSQLScript;
        }
    }
}
